/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 * Created on 4.5.2011, 9:20:50
 */

package src.clovece;

import java.awt.Point;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import src.clovece.game.GameInfo;
import src.clovece.game.GameManagement;
import src.clovece.game.Save;

/**
 *  Class serves as menu for choosing game graphic mode
 */
public class GraphicPreferencesDialog extends javax.swing.JDialog {
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
    private GameManagement gm = GameManagement.getGameManagementInstance();
    private GameFrame parent = null;
    private JLabel label1 = null;
    private JLabel label2 = null;
    private JLabel label3 = null;
    private JLabel label4 = null;
    
    /** Creates new form GraphicPreferencesDialog */
    public GraphicPreferencesDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        Point p = parent.getLocation();
        this.parent = (GameFrame) parent;
        this.setLocation(p.x +330, p.y +250);
    }

    /*
     * Initialises and shows dialog
     */
    public void showYourself() {
        initComponents();
        ButtonGroup group = new ButtonGroup();
        group.add(this.RadioButton_player1);
        group.add(this.RadioButton_player2);
        group.add(this.RadioButton_player3);
        group.add(this.RadioButton_player4);

        if(this.gameInfo.getChosenFigurine() == 0) this.RadioButton_player1.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 1) this.RadioButton_player2.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 2) this.RadioButton_player3.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 3) this.RadioButton_player4.setSelected(true);
        
        this.label1 = new JLabel();
        this.label2 = new JLabel();
        this.label3 = new JLabel();
        this.label4 = new JLabel();


        this.label1.setIcon(Figurine(this.gameInfo.getGraphicMode(), 0));
        this.label1.setBounds(45,20,40,40);

        this.label2.setIcon(Figurine(this.gameInfo.getGraphicMode(), 1));
        this.label2.setBounds(45,80,40,40);

        this.label3.setIcon(Figurine(this.gameInfo.getGraphicMode(), 2));
        this.label3.setBounds(130,20,40,40);

        this.label4.setIcon(Figurine(this.gameInfo.getGraphicMode(), 3));
        this.label4.setBounds(130,80,40,40);

        this.FigurinePanel.add(label1);
        this.FigurinePanel.add(label2);
        this.FigurinePanel.add(label3);
        this.FigurinePanel.add(label4);


        this.GraphicModeComboBox.setSelectedIndex(this.gameInfo.getGraphicMode());
        this.setVisible(true);
    }

    private ImageIcon Figurine(int gameSet, int figurine){
        ImageIcon icon = new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img"
                  + System.getProperty("file.separator") + "figurine" + gameSet + figurine + "a" + ".png");
        return icon;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        FigurinePanel = new javax.swing.JPanel();
        RadioButton_player1 = new javax.swing.JRadioButton();
        RadioButton_player3 = new javax.swing.JRadioButton();
        RadioButton_player2 = new javax.swing.JRadioButton();
        RadioButton_player4 = new javax.swing.JRadioButton();
        GraphicModeLabel = new javax.swing.JLabel();
        GraphicModeComboBox = new javax.swing.JComboBox();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        FigurinePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Choose your figurine", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        RadioButton_player1.setMnemonic(KeyEvent.VK_D);
        RadioButton_player1.setSelected(true);

        RadioButton_player3.setMnemonic(KeyEvent.VK_D);

        RadioButton_player2.setMnemonic(KeyEvent.VK_D);

        RadioButton_player4.setMnemonic(KeyEvent.VK_D);

        javax.swing.GroupLayout FigurinePanelLayout = new javax.swing.GroupLayout(FigurinePanel);
        FigurinePanel.setLayout(FigurinePanelLayout);
        FigurinePanelLayout.setHorizontalGroup(
            FigurinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(FigurinePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(FigurinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player1)
                    .addComponent(RadioButton_player2))
                .addGap(65, 65, 65)
                .addGroup(FigurinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(RadioButton_player3)
                    .addComponent(RadioButton_player4))
                .addGap(60, 60, 60))
        );
        FigurinePanelLayout.setVerticalGroup(
            FigurinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(FigurinePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(FigurinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player1)
                    .addComponent(RadioButton_player3))
                .addGap(33, 33, 33)
                .addGroup(FigurinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player2)
                    .addComponent(RadioButton_player4))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        GraphicModeLabel.setText("Graphic Mode:");

        GraphicModeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mode 1", "Mode 2" }));
        GraphicModeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GraphicModeComboBoxActionPerformed(evt);
            }
        });

        OKButton.setText("OK");
        OKButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                OKButtonMouseClicked(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(FigurinePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(GraphicModeLabel)
                    .addComponent(GraphicModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(OKButton, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(FigurinePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(GraphicModeLabel)
                        .addGap(11, 11, 11)
                        .addComponent(GraphicModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(OKButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CancelButton))
                        .addGap(43, 43, 43))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OKButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OKButtonMouseClicked
        // graphic mode
        if(this.GraphicModeComboBox.getSelectedItem().toString().equals("Mode 1")) {
            this.gameInfo.setGraphicMode(0);
        }
        else {
            this.gameInfo.setGraphicMode(1);
        }

        // chosen figurine
        if(this.RadioButton_player1.isSelected()) {
            this.gameInfo.setChosenFigurine(0);
            System.out.println("0");
        }
        else if(this.RadioButton_player2.isSelected()) {
            this.gameInfo.setChosenFigurine(1);
            System.out.println("1");
        }
        else if(this.RadioButton_player3.isSelected()) {
            this.gameInfo.setChosenFigurine(2);
            System.out.println("2");
        }
        else {
            this.gameInfo.setChosenFigurine(3);
            System.out.println("3");
        }

        if(this.gameInfo.getHasStarted()) this.gm.repaintBoard();
        Save save = new Save();
        this.dispose();
    }//GEN-LAST:event_OKButtonMouseClicked

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        this.dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    private void GraphicModeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GraphicModeComboBoxActionPerformed
        if(this.GraphicModeComboBox.getSelectedItem().toString().equals("Mode 1")) {
            this.setGraphicMode1();
        }
        else {
            this.setGraphicMode2();
        }
    }//GEN-LAST:event_GraphicModeComboBoxActionPerformed

    /*
     * Loads figurines from first graphic set
     */
    private void setGraphicMode1() {
        this.label1.setIcon(this.Figurine(0, 0));
        this.label2.setIcon(this.Figurine(0, 1));
        this.label3.setIcon(this.Figurine(0, 2));
        this.label4.setIcon(this.Figurine(0, 3));
    }

    /*
     * Loads figurines from second graphic set
     */
    private void setGraphicMode2() {
        this.label1.setIcon(this.Figurine(1, 0));
        this.label2.setIcon(this.Figurine(1, 1));
        this.label3.setIcon(this.Figurine(1, 2));
        this.label4.setIcon(this.Figurine(1, 3));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JPanel FigurinePanel;
    private javax.swing.JComboBox GraphicModeComboBox;
    private javax.swing.JLabel GraphicModeLabel;
    private javax.swing.JButton OKButton;
    private javax.swing.JRadioButton RadioButton_player1;
    private javax.swing.JRadioButton RadioButton_player2;
    private javax.swing.JRadioButton RadioButton_player3;
    private javax.swing.JRadioButton RadioButton_player4;
    // End of variables declaration//GEN-END:variables

}
